package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping("/admin/setmeal")
@RestController
@Slf4j
@Api(tags = "套餐相关接口")
public class SetmealController {
    @Autowired
    SetmealService setmealService;

    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public Result<PageResult> pageQuery( SetmealPageQueryDTO setmealPageQueryDTO) {

        log.info("用户查询套餐");

        PageResult pageResult= setmealService.pageQuery(setmealPageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 用户新增套餐
     * @return
     */
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    @ApiOperation("用户新增套餐")
    @PostMapping
    public Result insert(@RequestBody SetmealDTO setmealDTO) {

        log.info("用户新增套餐 {}", setmealDTO);

        setmealService.insert(setmealDTO);

        return Result.success();

    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> selectById(@PathVariable Long id) {
        log.info("根据id查询套餐 {}",id);

        SetmealVO setmealVO = setmealService.selectById(id);

        return Result.success(setmealVO);
    }

    @PutMapping
    @ApiOperation("更新套餐和套餐菜品")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result updateWithSetmealDish(@RequestBody SetmealDTO setmealDTO) {

    log.info("更新套餐表和套餐菜品表");

    setmealService.updateWithSetmealDish(setmealDTO);

    return Result.success();

    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @ApiOperation("批量删除套餐")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result deleteByIds(@RequestParam List<Long> ids) {

        log.info("批量删除套餐");

        setmealService.deleteByIds(ids);

        return Result.success();

    }

    /**
     * 启用禁用套餐
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result stopOrStart(@PathVariable Integer status,Long id) {

        log.info("启用禁用套餐 {}",status);

        setmealService.stopOrStart(status,id);

        return Result.success();

    }
}
